/*     */ package com.zimbra.cs.mailclient.util;
/*     */ 
/*     */ import java.beans.BeanInfo;
/*     */ import java.beans.Introspector;
/*     */ import java.beans.PropertyDescriptor;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.lang.reflect.Method;
/*     */ import java.util.Properties;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class Config
/*     */ {
/*     */   public static Properties toProperties(Object bean)
/*     */   {
/*  39 */     Properties props = new Properties();
/*     */     try {
/*  41 */       BeanInfo bi = Introspector.getBeanInfo(bean.getClass());
/*  42 */       for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
/*  43 */         if (isSupportedProperty(pd)) {
/*  44 */           Object value = pd.getReadMethod().invoke(bean, new Object[0]);
/*  45 */           if (value != null) {
/*  46 */             props.setProperty(pd.getName(), value.toString());
/*     */           }
/*     */         }
/*     */       }
/*     */     } catch (Exception e) {
/*  51 */       throw new IllegalArgumentException("Invalid config bean", e);
/*     */     }
/*  53 */     return props;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void applyProperties(Object bean, Properties props)
/*     */   {
/*     */     try
/*     */     {
/*  64 */       BeanInfo bi = Introspector.getBeanInfo(bean.getClass());
/*  65 */       for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
/*  66 */         String name = pd.getName();
/*  67 */         String value = props.getProperty(name);
/*  68 */         if ((value != null) && (isSupportedProperty(pd))) {
/*  69 */           Object obj = parseValue(value, pd.getPropertyType());
/*  70 */           pd.getWriteMethod().invoke(bean, new Object[] { obj });
/*     */         }
/*     */       }
/*     */     } catch (Exception e) {
/*  74 */       throw new IllegalArgumentException("Invalid bean", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Properties loadProperties(File file)
/*     */     throws IOException
/*     */   {
/*  86 */     FileInputStream is = new FileInputStream(file);
/*  87 */     Properties props = new Properties();
/*     */     try {
/*  89 */       props.load(is);
/*     */     } finally {
/*  91 */       is.close();
/*     */     }
/*  93 */     return props;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void saveProperties(File file, Properties props)
/*     */     throws IOException
/*     */   {
/* 105 */     FileOutputStream os = new FileOutputStream(file);
/*     */     try {
/* 107 */       props.store(os, null);
/*     */     } finally {
/* 109 */       os.close();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static boolean isSupportedProperty(PropertyDescriptor pd)
/*     */   {
/* 116 */     if ((pd.getReadMethod() == null) || (pd.getWriteMethod() == null)) {
/* 117 */       return false;
/*     */     }
/* 119 */     Class type = pd.getPropertyType();
/* 120 */     return (type == Long.class) || (type == Long.TYPE) || (type == Integer.class) || (type == Integer.TYPE) || (type == Short.class) || (type == Short.TYPE) || (type == Character.class) || (type == Character.TYPE) || (type == Byte.class) || (type == Byte.TYPE) || (type == Boolean.class) || (type == Boolean.TYPE) || (type == String.class) || (type.isEnum());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static Object parseValue(String value, Class type)
/*     */   {
/* 132 */     if ((type == Long.class) || (type == Long.TYPE))
/* 133 */       return Long.valueOf(Long.parseLong(value));
/* 134 */     if ((type == Integer.class) || (type == Integer.TYPE))
/* 135 */       return Integer.valueOf(Integer.parseInt(value));
/* 136 */     if ((type == Short.class) || (type == Short.TYPE))
/* 137 */       return Short.valueOf(Short.parseShort(value));
/* 138 */     if ((type == Character.class) || (type == Character.TYPE)) {
/* 139 */       if (value.length() != 1) {
/* 140 */         throw new IllegalArgumentException("Invalid character value: " + value);
/*     */       }
/*     */       
/* 143 */       return Character.valueOf(value.charAt(0)); }
/* 144 */     if ((type == Byte.class) || (type == Byte.TYPE))
/* 145 */       return Byte.valueOf(Byte.parseByte(value));
/* 146 */     if ((type == Boolean.class) || (type == Boolean.TYPE))
/* 147 */       return Boolean.valueOf(Boolean.parseBoolean(value));
/* 148 */     if (type == String.class)
/* 149 */       return value;
/* 150 */     if (type.isEnum()) {
/* 151 */       return Enum.valueOf(type, value);
/*     */     }
/* 153 */     return null;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/util/Config.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */